<?php
/**
 * Fang_Service_Photo
 * 
 * 作者:  姚刚 (yaogang@the9sense.com)
 * 创建时间: 2011-03-15 18:09:42
 * 修改记录: 
 * 
 * $Id: Photo.php 18 2011-04-17 12:50:07Z coosuu $
 */

class Fang_Service_Photo {

    /**
     * add 
     * 
     * @param  mixed $data 
     * @return void
     */
    public function add($data) {

        try {
            return Fang_DAO_Photo::add($data);
        } catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }
    }

    /**
     * update 
     * 
     * @param  mixed $pid 
     * @param  mixed $data 
     * @return void
     */
    public function update($pid, $data) {

        try {
            return Fang_DAO_Photo::update($pid, $data);
        } catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }
    }

    /**
     * updateByPids 
     * 
     * @param  mixed $pids 
     * @param  mixed $data 
     * @return void
     */
    public function updateByPids($pids, $data) {

        try {
            return Fang_DAO_Photo::updateByPids($pids, $data);
        } catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }
    }

    /**
     * remove 
     * 
     * @param  mixed $pid 
     * @return void
     */
    public function remove($pid) {

        try {
            return Fang_DAO_Photo::remove($pid);
        } catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }
    }

    /**
     * getInfo 
     * 
     * @param  mixed $pid 
     * @param  array $fields 
     * @return void
     */
    public function getInfo($pid, $fields = array()) {

        try {
            return Fang_DAO_Photo::getInfo($pid, $fields);
        } catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }
    }

    /**
     * listByPids 
     * 
     * @param  mixed $pids 
     * @param  array $fields 
     * @return void
     */
    public function listByPids($pids, $fields = array()) {

        try {
            return Fang_DAO_Photo::listByPids($pids, $fields);
        } catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }
    }

    /**
     * listAll 
     * 
     * @param  array $pageOptions 
     * @param  array $fields 
     * @return void
     */
    public function listAll(&$pageOptions = array(), $fields = array()) {

        try {
            if ($pageOptions) {
                if (!$pageOptions['totalItems']) {
                    $pageOptions['totalItems'] = Fang_DAO_Photo::countAll();
                }
                $pageOptions = Pager::convert($pageOptions);
            }

            return Fang_DAO_Photo::listAll($pageOptions, $fields);
        } catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }
    }

    /**
     * listByAid 
     * 
     * @param  mixed $aid 
     * @param  array $fields 
     * @return void
     */
    public function listByAid($aid, $fields = array()) {

        try {
            return Fang_DAO_Photo::listByAid($aid, $fields);
        } catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }
    }

    /**
     * upload 
     * 
     * @param  mixed $file 
     * @param  mixed $fileName 
     * @return void
     */
    public function upload($file, $fileName) {

        try {
            $path = $this->_upload($file, $fileName);
            if (!$path) {
                return false;
            }

            $data = array('p_thumb_src' => $path['thumb'],
                          'p_src' => $path['src']);
            return $this->add($data);
        } catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }
    }

    /**
     * _upload 
     * 
     * @param  mixed $file 
     * @param  mixed $fileName 
     * @return void
     */
    private function _upload($file, $fileName) {

        if (!$file || !$file['tmp_name']) {
            throw new Service_Exception('请选择上传的图片');
        }

        $file['name'] = $fileName;

        // 获取图片后缀
        $fileExt = substr($file['name'], strpos($file['name'], '.') + 1);

        // 去掉后缀
        $file['name'] = substr($file['name'], 0, strrpos($file['name'], '.'));

        // 检查图片类型
        $allowMimes = array('gif', 'jpg', 'jpeg', 'png');
        if (!in_array(strtolower($fileExt), $allowMimes)) {
            throw new Service_Exception('图片类型错误');
        }

        // 继续检查图片类型
        $picData = getimagesize($file['tmp_name']);
        if (!in_array($picData['mime'], array('image/jpeg', 'image/gif', 'image/png'))) {
            throw new Service_Exception('图片类型错误!');
        }

        // 判断文件上传错误
        if ($file['error'] > 0) {
            $errCode = $file['error'];
            switch ($errCode) {
                case UPLOAD_ERR_INI_SIZE :
                case UPLOAD_ERR_FORM_SIZE :
                    throw new Service_Exception('图片太大');
                case UPLOAD_ERR_PARTIAL :
                    throw new Service_Exception('文件只有部分上传');
                case UPLOAD_ERR_NO_FILE :
                    throw new Service_Exception('没有文件被上传');
                case UPLOAD_ERR_NO_TMP_DIR :
                case UPLOAD_ERR_CANT_WRITE :
                    throw new Service_Exception('系统错误');
            }
        }

        $uploadDir = APP_PATH . '/www/photos/';

        $newPicDir = $uploadDir . date('Y-m-d');
        if (!is_dir($newPicDir)) {
            @mkdir($newPicDir, true);
            @chmod($newPicDir, 755);
        }

        $random = time() . '.' . mt_rand();
        $newPicName = date('Y-m-d') . '/' . $random . '.' . $fileExt;
        $newPicSrc = $uploadDir . $newPicName;
        if (!@move_uploaded_file($file['tmp_name'], $newPicSrc)) {
            return false;
        }

        $thumbName = date('Y-m-d') . '/' . $random . '_thumb.' . $fileExt;
        try {
            $image = new Image($newPicSrc);
            $image->thumb(155, 155);
            $thumbSrc = $uploadDir . $thumbName;
            $image->save($thumbSrc);
        } catch (Exception $e) {
            throw new Service_Exception($e);
        }

        return array('src' => $newPicName,
                     'thumb' => $thumbName);
    }
}
